package com.thesis.client;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.thesis.object.DishDetailAddBill;
import com.thesis.object.ListBillDescription;
import com.thesis.utils.Utils;

public class BillDetailTableAdapter extends BaseAdapter{
	private Context context;
	private ListBillDescription result;
	private ArrayList<Integer> dishId;
	private ArrayList<DishDetailAddBill> arr_modify;
	int default_number;

	

	public BillDetailTableAdapter(Context context,ListBillDescription result,ArrayList<Integer> dishId,
			ArrayList<DishDetailAddBill> arr_modify) {
		
		this.context = context;
		this.result = result;
		this.dishId = dishId;
		this.arr_modify = arr_modify;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView;
		if (convertView == null) {
			rowView = new View(context);
			rowView = inflater.inflate(R.layout.item_bill_detail_table, parent, false);
		} else {
			rowView = (View) convertView;
		}
		default_number = 1;

		TextView tv_food = (TextView) rowView.findViewById(R.id.ibdt_tv_food);
		TextView tv_cost = (TextView) rowView.findViewById(R.id.ibdt_tv_cost);
		final TextView tv_number = (TextView) rowView.findViewById(R.id.ibdt_tv_number);
		ImageButton ib_bonus = (ImageButton) rowView.findViewById(R.id.ibdt_ib_bonus);
		ImageButton	ib_minus = (ImageButton) rowView.findViewById(R.id.ibdt_ib_minus);
		CheckBox cb_served = (CheckBox) rowView.findViewById(R.id.ibdt_cb_served);
		if (!result.getListBill().get(position).getServeTime().equals("null")){
			cb_served.setChecked(true);
			cb_served.setEnabled(false);
		}
		
//		ImageButton btn_bonus = (ImageButton) rowView.findViewById(R.id.bd_ib_bonus);
//		ImageButton btn_minus = (ImageButton) rowView.findViewById(R.id.bd_ib_minus);
		
		tv_food.setText(Utils.sharedInstance().getNameofFood(result.getListBill().get(position).getFoodId()));
		tv_cost.setText(""+(result.getListBill().get(position).getCost()+" đồng"));

		default_number  = result.getListBill().get(position).getNumber();
		tv_number.setText(""+default_number);
//		btn_bonus.setImageResource(R.drawable.button_bonus);
//		btn_minus.setImageResource(R.drawable.button_minus);
		
		ib_bonus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				default_number ++;
				tv_number.setText(""+default_number);				
			}
		});
		
		ib_minus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (!result.getListBill().get(position).getServeTime().equals("null")){
					Utils.sharedInstance().showShortToast(context,"Món này đã được phục vụ");
				}
				else {
					if (default_number > 0){
						default_number -- ; 
						tv_number.setText(""+default_number);
					} 										
				}				
			}
		});
		cb_served.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked){
					dishId.add(result.getListBill().get(position).getFoodId());
					
				}
				else {
					for (int i = 0 ; i < dishId.size(); i++){
						if (result.getListBill().get(position).getFoodId() == dishId.get(i)){
							dishId.remove(i);
							
							break;
						}
					}
				}
			}
		});
		
		ib_bonus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				int cur = Integer.parseInt(tv_number.getText().toString());
//				for (int i = 0 ; i < arr_modify.size();i++){
//					if (result.getListBill().get(position).getFoodId() == arr_modify.get(i).getId()){
//						cur = arr_modify.get(i).getNumber();
//						break;
//					}
//				}
//				if (cur == 0 ) cur = result.getListBill().get(position).getNumber();
//				 cur = result.getListBill().get(position).getNumber();
				if (!result.getListBill().get(position).getServeTime().equals("null")){
					Utils.sharedInstance().showShortToast(context, "ko thay doi");
				}else{
					
					boolean check = false;
					cur = cur +1 ;
					for (int i = 0 ; i < arr_modify.size();i++){
						if (result.getListBill().get(position).getFoodId() == arr_modify.get(i).getId()){
							
							arr_modify.set(i, new DishDetailAddBill(result.getListBill().get(position).getFoodId() ,cur, result.getListBill().get(position).getFoodNote()));
							check = true;
							break;
						}																			
					}
					if (!check){
					
						arr_modify.add(new DishDetailAddBill(result.getListBill().get(position).getFoodId(),cur, result.getListBill().get(position).getFoodNote()));						
					}
					
					tv_number.setText(""+cur);
				}
				
			}
		});
		ib_minus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				int cur = 0;
				for (int i = 0 ; i < arr_modify.size();i++){
					if (result.getListBill().get(position).getFoodId() == arr_modify.get(i).getId()){
						cur = arr_modify.get(i).getNumber();
						break;
					}
				}
				if (cur == 0 ) cur = result.getListBill().get(position).getNumber();
				if (!result.getListBill().get(position).getServeTime().equals("null")){
					Utils.sharedInstance().showShortToast(context, "ko thay doi");
				}else{
					
					
						boolean check = false;
						for (int i = 0 ; i < arr_modify.size();i++){
							if (result.getListBill().get(position).getFoodId() == arr_modify.get(i).getId()){
								if (cur > 1 ) cur-- ;
								
								arr_modify.set(i, new DishDetailAddBill(result.getListBill().get(position).getFoodId() , cur, result.getListBill().get(position).getFoodNote()));
								check = true;
								break;
							}																			
						}
						if (!check){
							if (cur > 1) {
								cur--;
								arr_modify.add(new DishDetailAddBill(result.getListBill().get(position).getFoodId(),cur , result.getListBill().get(position).getFoodNote()));		
							}							
							else {
								
							}
						}
						tv_number.setText(""+cur);						
				}
				
			}
		});
		return rowView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return result.getListBill().size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}
}
